/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import junit.framework.TestCase;

/**
 * Performs Validation Test for url validations.
 *
 * @version $Revision: 1128446 $ $Date: 2011-05-27 13:29:27 -0700 (Fri, 27 May 2011) $
 */
public class UrlValidatorTest extends TestCase {

   private boolean printStatus = false;
   private boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using.

   public UrlValidatorTest(String testName) {
      super(testName);
   }

   public void testManualTest()
   {
	   String url;
	   boolean result, testPassed;
	   
	   testPassed = true;
	   
	   UrlValidator urlValidator = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);

	   // bug is at line 158 of UrlValidator.java
	   // private static final String PORT_REGEX = "^:(\\d{1,3})$";
	   // regex pattern matches only three digit port, not five digit port
       url = "http://www.google.com:65535";
       result = urlValidator.isValid(url);
       if (!result) {
    	   testPassed = false;
	       System.out.println("url: " + url);
	       System.out.println("result: " + result + "\n");
       }
       
       // bug is at line 446 of UrlValidator.java
       // return !QUERY_PATTERN.matcher(query).matches();
       // should be returning QUERY_PATTERN.matcher(query).matches()
       // rather than the opposite value !QUERY_PATTERN.matcher(query).matches()
	   url = "http://www.google.com?action=view";
       result = urlValidator.isValid(url);
       if (!result) {
    	   testPassed = false;
	       System.out.println("url: " + url);
	       System.out.println("result: " + result + "\n");
       }

       // bug is at line 96 of InetAddressValidator.java
       // the if statement "if (iIpSegment > 255)" should return false instead of true
       url = "http://256.256.256.256";
       result = urlValidator.isValid(url);
       if (result) {
    	   testPassed = false;
	       System.out.println("url: " + url);
	       System.out.println("result: " + result + "\n");
       }
       
       assertTrue(testPassed);
   }

   public void testFirstPartition()
   {
	   String url;
	   boolean result, testPassed;
	   
	   testPassed = true;
	   
	   UrlValidator urlValidator = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   
       url = "http://www.google.com";
       result = urlValidator.isValid(url);
       
       if (!result) {
    	   testPassed = false;
	       System.out.println("url: " + url);
	       System.out.println("result: " + result + "\n");
       }
       
       url = "https://www.google.com";
       result = urlValidator.isValid(url);
       
       if (!result) {
    	   testPassed = false;
	       System.out.println("url: " + url);
	       System.out.println("result: " + result + "\n");
       }
       
       assertTrue(testPassed);
   }
   
   public void testSecondPartition()
   {
	   String url;
	   boolean result, testPassed;
	   
	   testPassed = true;
	   
	   UrlValidator urlValidator = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	 
       url = "http://74.125.224.72";
       result = urlValidator.isValid(url);
       
       if (!result) {
    	   testPassed = false;
	       System.out.println("url: " + url);
	       System.out.println("result: " + result + "\n");
       }
       
       assertTrue(testPassed);
   }

   public void testThirdPartition()
   {
	   String url;
	   boolean result, testPassed;
	   
	   testPassed = true;
	   
	   UrlValidator urlValidator = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   
       url = "www.google.com";
       result = urlValidator.isValid(url);
       
       if (result) {
    	   testPassed = false;
	       System.out.println("url: " + url);
	       System.out.println("result: " + result + "\n");
       }
       
       assertTrue(testPassed);
   }   

   public void testFourthPartition()
   {
	   String url;
	   boolean result, testPassed;
	   
	   testPassed = true;
	   
	   UrlValidator urlValidator = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   
       url = "http://www.google.com ";
       result = urlValidator.isValid(url);
       
       if (!result) {
    	   testPassed = false;
	       System.out.println("url: " + url);
	       System.out.println("result: " + result + "\n");
       }
       
       assertTrue(testPassed);
   }

   public void testFifthPartition()
   {
	   String url;
	   boolean result, testPassed;
	   
	   testPassed = true;
	   
	   UrlValidator urlValidator = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   
       url = "http://www.google.com.edu";
       result = urlValidator.isValid(url);
       
       if (!result) {
    	   testPassed = false;
	       System.out.println("url: " + url);
	       System.out.println("result: " + result + "\n");
       }
       
       assertTrue(testPassed);
   }

   public void testSixthPartition()
   {
	   String url;
	   boolean result, testPassed;
	   
	   testPassed = true;
	   
	   UrlValidator urlValidator = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   
       url = "http://www.google.com?action=view%20all";
       result = urlValidator.isValid(url);
       
       if (!result) {
    	   testPassed = false;
	       System.out.println("url: " + url);
	       System.out.println("result: " + result + "\n");
       }
       
       assertTrue(testPassed);
   }

   public void testSeventhPartition()
   {
	   String url;
	   boolean result, testPassed;
	   
	   testPassed = true;
	   
	   UrlValidator urlValidator = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   
       url = "https://piazza.com/class?cid=80";
       result = urlValidator.isValid(url);
       
       if (!result) {
    	   testPassed = false;
	       System.out.println("url: " + url);
	       System.out.println("result: " + result + "\n");
       }
       
       assertTrue(testPassed);
   }

   public void testEighthPartition()
   {
	   String url;
	   boolean result, testPassed;
	   
	   testPassed = true;
	   
	   UrlValidator urlValidator = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   
       url = "http://www.google.com:65535";
       result = urlValidator.isValid(url);
       
       if (!result) {
    	   testPassed = false;
	       System.out.println("url: " + url);
	       System.out.println("result: " + result + "\n");
       }
       
       url = "http://www.google.com:6553565535";
       result = urlValidator.isValid(url);
       
       if (result) {
    	   testPassed = false;
	       System.out.println("url: " + url);
	       System.out.println("result: " + result + "\n");
       }
       
       assertTrue(testPassed);
   }

   public void testIsValid()
   {
	   UrlValidator urlValidator = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	   String url, scheme, authority, port, path, query;
       boolean expected, result, testPassed;
       
       testPassed = true;
       
       for (int i = 0; i < testUrlScheme.length; i++)
       {
    	   scheme = testUrlScheme[i].item;
    	   
    	   for (int j = 0; j < testUrlAuthority.length; j++)
    	   {
        	   authority = testUrlAuthority[j].item;
        	   
        	   for (int k = 0; k < testUrlPort.length; k++)
        	   {
            	   port = testUrlPort[k].item;     
            	   
            	   for (int l = 0; l < testPath.length; l++)
            	   {
            		   path = testPath[l].item;
            		   
                	   for (int m = 0; m < testUrlQuery.length; m++)
                	   {
                		   query = testUrlQuery[m].item;
                		   
        	   			   url = scheme + authority + port + path + query;
        	   			   expected = testUrlScheme[i].valid && testUrlAuthority[j].valid && 
        	   					   		testUrlPort[k].valid && testPath[l].valid && testUrlQuery[m].valid;
                           result = urlValidator.isValid(url);
                           
                           if (expected != result)
                           {
                        	   testPassed = false;
	        	   			   System.out.println("url: " + url);
	        	   			   System.out.println("expected: " + expected);
	        	   			   System.out.println("result: " + result + "\n");
                           }
                	   }
            	   }
        	   }
    	   }
    	   
    	   assertTrue(testPassed);
       }

	   for(int i = 0; i < 10000; i++)
	   {
		   
	   }
   }

   public void testAnyOtherUnitTest()
   {
	   
   }

   ResultPair[] testUrlScheme = {new ResultPair("http://", true),
                               new ResultPair("ftp://", true),
                               new ResultPair("h3t://", true),
                               new ResultPair("3ht://", false),
                               new ResultPair("http:/", false),
                               new ResultPair("http:", false),
                               new ResultPair("http/", false),
                               new ResultPair("://", false),
                               new ResultPair("", true)};

   ResultPair[] testUrlAuthority = {new ResultPair("www.google.com", true),
                                  new ResultPair("go.com", true),
                                  new ResultPair("go.au", true),
                                  new ResultPair("0.0.0.0", true),
                                  new ResultPair("255.255.255.255", true),
                                  new ResultPair("256.256.256.256", false),
                                  new ResultPair("255.com", true),
                                  new ResultPair("1.2.3.4.5", false),
                                  new ResultPair("1.2.3.4.", false),
                                  new ResultPair("1.2.3", false),
                                  new ResultPair(".1.2.3.4", false),
                                  new ResultPair("go.a", false),
                                  new ResultPair("go.a1a", false),
                                  new ResultPair("go.cc", true),
                                  new ResultPair("go.1aa", false),
                                  new ResultPair("aaa.", false),
                                  new ResultPair(".aaa", false),
                                  new ResultPair("aaa", false),
                                  new ResultPair("", false)
   };
   ResultPair[] testUrlPort = {new ResultPair(":80", true),
                             new ResultPair(":65535", true),
                             new ResultPair(":0", true),
                             new ResultPair("", true),
                             new ResultPair(":-1", false),
                             new ResultPair(":65636", true),
                             new ResultPair(":65a", false)
   };
   ResultPair[] testPath = {new ResultPair("/test1", true),
                          new ResultPair("/t123", true),
                          new ResultPair("/$23", true),
                          new ResultPair("/..", false),
                          new ResultPair("/../", false),
                          new ResultPair("/test1/", true),
                          new ResultPair("", true),
                          new ResultPair("/test1/file", true),
                          new ResultPair("/..//file", false),
                          new ResultPair("/test1//file", false)
   };
   ResultPair[] testUrlQuery = {new ResultPair("?action=view", true),
                              new ResultPair("?action=edit&mode=up", true),
                              new ResultPair("", true)
   };
}
